package com.ht.controller;


import cn.hutool.json.JSONUtil;
import com.ht.data.Result;
import com.ht.dto.Patient;
import com.ht.dto.User;
import com.ht.service.IPatientService;
import com.ht.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 代齐园
 * @since 2022-05-25
 */
@RestController
@RequestMapping("/patient")
@Api("关于患者的增删改查")//Swagger2的注解
public class PatientController {
    @Resource
    private IPatientService patientService;


    @GetMapping("/getPatient")
    //value为这个方法干什么用的
    @ApiOperation(value = "患者管理的查询")
    //name是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header")
    public Result getPatient(@RequestHeader("AuthToken")String token){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);//通过请求头获取登录用户
        List<Patient> patient = patientService.getPatient(user.getId());
        return Result.OK().setData(patient);
    }
    @PostMapping("/insertPatient")
    //value为这个方法干什么用的
    @ApiOperation(value = "患者管理的增加")
    //name是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header"),
            @ApiImplicitParam(name = "patient",value = "增加的患者信息",required = true,paramType = "body")
    })
    public Result insertPatient(@RequestHeader("AuthToken")String token,@RequestBody Patient patient){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);//通过请求头获取登录用户
        patientService.insertPatient(user.getId(),patient);
        return Result.OK().setData("患者信息增加成功");
    }
    @PutMapping("/updatePatient")
    //value为这个方法干什么用的
    @ApiOperation(value = "修改患者信息")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "patient",value = "用户修改数据",required = true,paramType = "body")
    })
    public Result updatePatient(@RequestHeader("AuthToken")String token,@RequestBody Patient patient){
        patientService.updatePatient(patient);
        return Result.OK().setData("密码修改成功");
    }
    @DeleteMapping("/deletePatient/{id}")
    //value为这个方法干什么用的
    @ApiOperation(value = "根据id删除患者信息")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParam(name = "id",value = "患者信息id",required = true,paramType = "path")
    public Result deletePatient(@PathVariable("id") Long id){
        patientService.deletePatient(id);
        return Result.OK();
    }

}